<template>
	<view class="content">
		<view class="row b-b">
			<text class="tit">联系人</text>
			<input class="input" type="text" :value="addressData.userName" @input="inpugChange" data-key='userName' placeholder="收货人姓名"
			 placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">手机号</text>
			<input class="input" type="number" :value="addressData.userPhone" @input="inpugChange" data-key='userPhone'
			 placeholder="收货人手机号码" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">所在地区</text>
			<view @click="chooseLocation" class="input">
				{{addressData.areaName}}
			</view>
			<!-- <text class="yticon icon-shouhuodizhi"></text> manageType=='edit'?addressData.userAddress.area: -->
		</view>
		<view class="row b-b">
			<text class="tit">详细地址</text>
			<input class="input" type="text" :value="addressData.userAddress" @input="inpugChange" data-key='userAddress'
			 placeholder="请输入您的详细地址" placeholder-class="placeholder" />
		</view>

		<view class="row default-row">
			<text class="tit">设为默认</text>
			<switch :checked="addressData.isDefault==1" color="#fa436a" @change="switchChange" />
		</view>
		<button class="add-btn" @click="confirm">提交</button>
		<selectAddress ref='selectAddress' @selectAddress="successSelectAddress"></selectAddress>
	</view>
</template>

<script>
	import selectAddress from '@/components/selectAddress/selectAddress.vue'
	export default {
		components: {
			selectAddress
		},
		data() {
			return {
				allow: true,
				manageType: '',
				addressData: {
					userName: '',
					userPhone: '',
					areaName: '请点击选择地区',
					userAddress: '',
					isDefault: 0,
					areaId: 0,
				}
			}
		},
		onLoad(option) {
			let title = '新增收货地址';
			if (option.type === 'edit') {
				title = '编辑收货地址'
				this.addressData = JSON.parse(option.data)
				console.log(this.addressData)
			}
			this.manageType = option.type;
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
			inpugChange(e) {
				let key = e.currentTarget.dataset.key;
				// if (this.manageType == 'edit' && key == "userAddress") {
				// 	this.addressData.userAddress[key] = e.detail.value
				// } else {
				this.addressData[key] = e.detail.value
				// }
			},
			successSelectAddress(address) {
				console.log({
					address
				})
				// if (this.manageType == 'edit') {
				// 	this.addressData.userAddress.area
				// } else {

				this.addressData.areaName = address.text
				// }
				console.log(address)
				this.addressData.areaId = address.obj[0]
			},
			switchChange(e) {
				console.log(e.detail)
				this.addressData.isDefault = e.detail ? 1 : 0;
			},

			//地图选择地址
			chooseLocation() {
				// uni.chooseLocation({
				// 	success: (data) => {
				// 		this.addressData.addressName = data.name;
				// 		this.addressData.address = data.name;
				// 	}
				// })
				this.$refs.selectAddress.show()
			},

			//提交
			confirm() {

				let data = {};
				if (this.manageType == 'edit') {
					data = {
						userName: this.addressData.userName,
						userPhone: this.addressData.userPhone,
						userAddress: this.addressData.userAddress,
						area: this.addressData.areaName,
						isDefault: this.addressData.isDefault,
						addressId: this.addressData.addressId,
						areaId: this.addressData.areaId
					}
				} else {
					data = {
						userName: this.addressData.userName,
						userPhone: this.addressData.userPhone,
						userAddress:this.addressData.userAddress,
						areaId:  this.addressData.areaId,
						isDefault: this.addressData.isDefault,
					}
				}
				console.log(data)
				if (!data.userName || data.userName == '') {
					this.$api.msg('请填写收货人姓名');
					return;
				}
				if (!/^1[3456789]\d{9}$/.test(data.userPhone)) {
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if (!data.userAddress) {
					this.$api.msg('请填写详细地址信息');
					return;
				}
				let that = this
				console.log(that.manageType)
				if (that.manageType == 'edit') {
					that.$Request.request('/api/address/change_address', 'POST', data).then(res => {
						if (res.status == 1) {
							that.$api.msg(res.msg)
							setTimeout(() => {
								uni.navigateBack()
							}, 800)
						}
						console.log(res)
					}).catch(err => {
						console.log(err)
					})
				} else {

					that.$Request.request('/api/address/create_address', 'POST', data).then(res => {
						if (res.status == 1) {
							that.$api.msg(res.msg)
							setTimeout(() => {
								uni.navigateBack()
							}, 800)
						}
						console.log(res)

					}).catch(err => {
						console.log(err)
					})
				}
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-top: 16upx;
	}

	.row {
		display: flex;
		align-items: center;
		position: relative;
		padding: 0 30upx;
		height: 110upx;
		background: #fff;

		.tit {
			flex-shrink: 0;
			width: 120upx;
			font-size: $uni-font-size-sm;
			color: $font-color-dark;
		}

		.input {
			flex: 1;
			font-size: $uni-font-size-base;
			color: $font-color-dark;
		}

		.icon-shouhuodizhi {
			font-size: 36upx;
			color: $font-color-light;
		}
	}

	.default-row {
		margin-top: 16upx;

		.tit {
			flex: 1;
			font-size: $uni-font-size-sm;
		}

		switch {
			transform: translateX(16upx) scale(.9);
		}
	}

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
